﻿@{
    Layout = "~/Views/Shared/_FormWhite.cshtml";
}

<div class="wrapper animated fadeInRight">
    <form id="form" class="form-horizontal m">
        <div class="form-group">
            <label class="col-sm-3 control-label ">开始日期<font class="red"> *</font></label>
            <div class="col-sm-4">
                <input id="startDate" col="startDate" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">结束日期<font class="red"> *</font></label>
            <div class="col-sm-4">
                <input id="endDate" col="endDate" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">选择服务项目<font class="red"> *</font></label>
            <div class="col-sm-4">
                <div id="productId" col="ProductId"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">数量<font class="red"> *</font></label>
            <div class="col-sm-4">
                <input id="count" col="Count" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">选择客户<font class="red"> *</font></label>
            <div class="col-sm-8">
                <div id="accountId" col="AccountId"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">客户地址<font class="red"> *</font></label>
            <div class="col-sm-8">
                <input id="accountAddr" col="AccountAddr" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">选择设备<font class="red"> *</font></label>
            <div class="col-sm-8">
                <div class="col-sm-10" id="deviceName" col="DeviceName"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">选择人员<font class="red"> *</font></label>
            <div class="col-sm-8">
                <div class="col-sm-10" id="userId" col="UserId"></div>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var id = ys.request("id");
    var accountName = '';
    $(function () {
        $('#form').validate({
            rules: {
                startDate: { required: true },
                endDate: { required: true },
                productId: { required: true },
                deviceName: { required: true },
                count: { required: true },
                accountId: { required: true },
                accountAddr: { required: true },
                userId: { required: true }
            }
        });

        laydate.render({
            elem: '#startDate',
            done: function (value, date, endDate) {
                b.config.min = {
                    year: date.year,
                    month: date.month - 1,
                    date: date.date,
                    hours: date.hours,
                    minutes: date.minutes,
                    seconds: date.seconds
                }
            }
        });
        var b = laydate.render({
            elem: '#endDate',
        });

        $("#productId").ysComboBox({
            url: '@Url.Content("~/ProductManage/Product/GetListJson")',
            key: "Id",
            value: "ProductName",
            class: "form-control",
            onChange: function () {
                var date = $("#startDate").val();
                var enddate = $("#endDate").val();
                ys.ajax({
                    url: '@Url.Content("~/ProductManage/ProductDevice/GetNOListJson")' + '?ProductId=' + this.value + '&startdate=' + date + '&enddate=' + enddate,
                    type: 'get',
                    success: function (obj) {
                        var dName;
                        if (obj.Tag == 1) {
                            for (var r in obj.Result) {
                                if (dName == obj.Result[r].DeviceName) {
                                    obj.Result[r].IsMustDevice = false;
                                }
                                dName = obj.Result[r].DeviceName;
                                obj.Result[r].DeviceName += ":" + obj.Result[r].DeviceNO;
                            }
                            $("#deviceName").ysCheckBox({
                                data: obj.Result,
                                dataName: "Result",
                                key: "DeviceName",
                                value: "DeviceName",
                                select: "IsMustDevice"
                            });
                        }
                    }
                });

                $("#userId").ysCheckBox({
                    url: '@Url.Content("~/ProductManage/ProductUser/GetListJson")' + '?ProductId=' + this.value,
                    dataName: "Result",
                    key: "UserId",
                    value: "UserName"
                });
            }
        });

        ys.ajax({
            url: '@Url.Content("~/OrganizationManage/Account/GetListJson")',
            type: 'get',
            success: function (obj) {
                if (obj.Tag == 1) {
                    var ds = obj.Result
                    $("#accountId").ysComboBox({
                        key: "Id",
                        value: "AccountName",
                        class: "form-control",
                        data: ds,
                        onChange: function () {
                            for (var d in ds) {
                                if (ds[d].Id == this.value) {
                                    $("#accountAddr").val(ds[d].AccountAddr);
                                    accountName = ds[d].AccountName;
                                }
                            }
                        }
                    });
                }
                else {
                    ys.msgError(obj.Message);
                }
            }
        });
    });

    function saveForm(index) {
        if ($('#form').validate().form()) {
            var postData = $('#form').getWebControls({ Id: id });
            postData.AccountName = accountName;
            ys.ajax({
                url: '@Url.Content("~/ScheduleManage/Schedule/SaveFormJson")',
                type: 'post',
                data: postData,
                success: function (obj) {
                }
            });
            ys.ajax({
                url: '@Url.Content("~/ScheduleManage/ScheduleUser/SaveFormJson")',
                type: 'post',
                data: postData,
                success: function (obj) {
                    if (obj.Tag == 1) {
                        ys.msgSuccess(obj.Message);
                        parent.searchGrid();
                        parent.layer.close(index);
                    }
                    else {
                        ys.msgError(obj.Message);
                    }
                }
            });
        }
    }
</script>

